package com.CatTalkApi.Servlet;

import com.CatTalkApi.DAO.DataBaseContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet("/DatabaseTest")
public class DatabaseTestServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        boolean rest = true;
        int count = 0;
        String ID1 = req.getParameter("ID1"), ID2 = req.getParameter("ID2");
        ResultSet resultSet = null;
        try {

            String sql = "SELECT * FROM `usrinfo` WHERE Name = '%s' And UsrPwd = '%s'";
            System.out.println("执行的SQL语句" + String.format(sql, ID1, ID2));
            resultSet = DataBaseContext.getInstance().GetStmt().executeQuery(String.format(sql, ID1, ID2));
            resultSet.last();
            resp.getWriter().write("DO Done.: " + resultSet.getRow());

            resultSet.close();
            DataBaseContext.getInstance().Close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            rest = false;
            resp.getWriter().write("RunError!" + throwables.getMessage());
        }
        resp.setCharacterEncoding("UTF-8");
    }
}
